package com.gz.rabbitmq.controller;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("api/mq/")
public class Test {

    @Value("${rabbitmq.exchange-name}")
    String EX_CHANGE_NAME;

    @Value("${rabbitmq.queue}")
    String QUEUE_NAME;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @PostMapping("send")
    public void send() {
        rabbitTemplate.convertAndSend(EX_CHANGE_NAME, "item.boot", "hello springboot");
    }

    @PostMapping("sendTTL")
    public void sendTTL() {
        MessageProperties properties = new MessageProperties();
        properties.setExpiration("8000");
        Message message = new Message("测试过期时间".getBytes(), properties);
        rabbitTemplate.convertAndSend("TTL_Exchange", "ttl", message);
    }
}
